using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// ConsumerList 的摘要说明。
	/// </summary>
	public class ConsumerList : BaseDAL<ConsumerListInfo>, IConsumerList
	{
		#region 对象实例及构造函数

		public static ConsumerList Instance
		{
			get
			{
				return new ConsumerList();
			}
		}
		public ConsumerList() : base("KF_ConsumerList","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override ConsumerListInfo DataReaderToEntity(IDataReader dataReader)
		{
			ConsumerListInfo consumerListInfo = new ConsumerListInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			consumerListInfo.ID = reader.GetInt32("ID");
            consumerListInfo.Order_ID = reader.GetInt32("Order_ID");
			consumerListInfo.RoomNo = reader.GetString("RoomNo");
            consumerListInfo.ItemType = reader.GetString("ItemType");
			consumerListInfo.ItemName = reader.GetString("ItemName");
            consumerListInfo.ItemNo = reader.GetString("ItemNo");
			consumerListInfo.Price = reader.GetDecimal("Price");
			consumerListInfo.Discount = reader.GetDouble("Discount");
            consumerListInfo.DiscountPrice = reader.GetDecimal("DiscountPrice");
			consumerListInfo.Quantity = reader.GetDouble("Quantity");
			consumerListInfo.Amount = reader.GetDecimal("Amount");
			consumerListInfo.BeginTime = reader.GetDateTime("BeginTime");
			consumerListInfo.EndTime = reader.GetDateTime("EndTime");
			consumerListInfo.Waiter = reader.GetString("Waiter");
			consumerListInfo.Creator = reader.GetString("Creator");
            consumerListInfo.Account = reader.GetString("Account");
            consumerListInfo.PayType = reader.GetString("PayType");
            consumerListInfo.IsCheckout = reader.GetInt32("IsCheckout");
			
			return consumerListInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(ConsumerListInfo obj)
		{
		    ConsumerListInfo info = obj as ConsumerListInfo;
			Hashtable hash = new Hashtable();

            hash.Add("Order_ID", info.Order_ID);
 			hash.Add("RoomNo", info.RoomNo);
            hash.Add("ItemType", info.ItemType);
 			hash.Add("ItemName", info.ItemName);
            hash.Add("ItemNo", info.ItemNo);
 			hash.Add("Price", info.Price);
 			hash.Add("Discount", info.Discount);
            hash.Add("DiscountPrice", info.DiscountPrice);
 			hash.Add("Quantity", info.Quantity);
 			hash.Add("Amount", info.Amount);
 			hash.Add("BeginTime", info.BeginTime);
 			hash.Add("EndTime", info.EndTime);
 			hash.Add("Waiter", info.Waiter);
 			hash.Add("Creator", info.Creator);
            hash.Add("Account", info.Account);
            hash.Add("IsCheckout", info.IsCheckout);
            hash.Add("PayType", info.PayType);
 				
			return hash;
		}

        /// <summary>
        /// 获取该订单的相关消费记录
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public List<ConsumerListInfo> GetGoodsConsumption(int orderId)
        {
            string sql = string.Format(@"Select * from {0} Where Order_ID ={1}", tableName, orderId);
            ConsumerListInfo entity = null;
            List<ConsumerListInfo> list = new List<ConsumerListInfo>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entity = DataReaderToEntity(dr);
                    list.Add(entity);
                }
            }
            return list;
        }

        /// <summary>
        /// 更新消费结账状态
        /// </summary>
        /// <param name="account"></param>
        /// <param name="checkFlag"></param>
        /// <returns></returns>
        public bool UpdateConsumeStatus(string account, int checkFlag, string payType)
        {

            string strSql = "update KF_ConsumerList set  IsCheckout=" + checkFlag + ",PayType='" + payType + "'  where Account='" + account + "'";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(strSql);
            return db.ExecuteNonQuery(command) > 0;
        
        }
    }
}